package com.dzt.cloudreader.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.dzt.cloudreader.R;
import com.dzt.cloudreader.activity.DoubanTopActivity;
import com.dzt.cloudreader.adapter.HotMovieAdapter;
import com.dzt.cloudreader.base.LoadingFragment;
import com.dzt.cloudreader.constant.ConstantsImageUrl;
import com.dzt.cloudreader.interfaces.PerfectClickListener;
import com.dzt.cloudreader.model.HotMovieBean;
import com.dzt.cloudreader.network.HotMoviePresenter;
import com.dzt.cloudreader.network.HotMovieView;
import com.dzt.cloudreader.utils.ImgLoadUtil;
import com.dzt.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by M02323 on 2017/12/15.
 */

public class OneFragment extends LoadingFragment {
	@BindView(R.id.list_one)
	XRecyclerView recyclerView;
	private View mHeaderView = null;
	private HotMovieAdapter adapter;
	private List<HotMovieBean.SubjectsBean> lists = new ArrayList<>();
	private HotMoviePresenter presenter;
	private LayoutInflater inflater;

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_one;
	}

	@Override
	protected void initData() {
		inflater = LayoutInflater.from(context);
		adapter = new HotMovieAdapter(context, lists, R.layout.item_one);
		initRecyclerView();
		presenter = new HotMoviePresenter(context);
		presenter.onCreate();
		presenter.attachView(movieView);
		presenter.getHotMovie();
	}

	private void initRecyclerView(){
		recyclerView.setPullRefreshEnabled(false);
		recyclerView.setLoadingMoreEnabled(false);
		recyclerView.clearHeader();
		mHeaderView = inflater.inflate(R.layout.header_item_one, null, false);
		View llMovieTop = mHeaderView.findViewById(R.id.ll_movie_top);
		ImageView ivImg = mHeaderView.findViewById(R.id.iv_img);
		ImgLoadUtil.displayRandom(3, ConstantsImageUrl.ONE_URL_01,ivImg);
		llMovieTop.setOnClickListener(new PerfectClickListener() {
			@Override
			protected void onNoDoubleClick(View v) {
				DoubanTopActivity.start(v.getContext());
			}
		});
		recyclerView.addHeaderView(mHeaderView);
		recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
		// 需加，不然滑动不流畅
		recyclerView.setNestedScrollingEnabled(false);
		recyclerView.setHasFixedSize(false);
		recyclerView.setItemAnimator(new DefaultItemAnimator());
		recyclerView.setAdapter(adapter);
	}

	@Override
	protected void onRefresh() {
		super.onRefresh();
		presenter.getHotMovie();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		if(presenter != null){
			presenter.onDestroy();
		}
	}

	private HotMovieView movieView = new HotMovieView() {
		@Override
		public void onSuccess(HotMovieBean movie) {
			showContentView();
			lists.addAll(movie.getSubjects());
			adapter.notifyDataSetChanged();
		}

		@Override
		public void onError(String result) {

		}
	};
}
